package com.tree.security.config;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * 自定义 WEB 服务器参数 可以配置默认错误页面
 *
 * @author tree
 */
@Configuration
public class WebServerAutoConfiguration {

    /**
     * * 2020-02-08 12:20:20 [main] TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping -
     * * 	o.s.b.a.w.s.e.BasicErrorController:
     * *        { /error}: error(HttpServletRequest)
     * *    { /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
     * *
     *
     * @return
     */
    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
		/*
		利用webServer设置ERROR PAGE的跳转页面， 最终使用ErrorController来实现真正跳转
		 */
        ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400");
        ErrorPage errorPage401 = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401");
        ErrorPage errorPage403 = new ErrorPage(HttpStatus.FORBIDDEN, "/error/403");
        ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
        ErrorPage errorPage415 = new ErrorPage(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "/error/415");
        ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
        factory.addErrorPages(errorPage400, errorPage401, errorPage403, errorPage404, errorPage415, errorPage500);
        return factory;
    }

}
